package org.cloud.shop.gateway.security.config;

import org.apache.catalina.authenticator.SpnegoAuthenticator.AuthenticateAction;
import org.cloud.shop.gateway.security.MyPasswordEncoder;
import org.cloud.shop.gateway.security.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	private MyUserService userService;
	
	private MyPasswordEncoder passwordEncoder;
	
	public void configure(AuthenticationManagerBuilder auth) throws Exception{
		//1.内存中简单配置
		//auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
		//2.使用数据库中的数据校验
	//	userService.
		auth.userDetailsService(userService).passwordEncoder(passwordEncoder);
	}
	
	@Override
	public void configure(HttpSecurity http) throws Exception {

		http.csrf().disable()
		    .authorizeRequests()
		    .antMatchers("/").permitAll()
		    .anyRequest().authenticated().and()
			.logout().permitAll().and()
			.formLogin();

	}

	@Override
	public void configure(WebSecurity web) throws Exception {

		web.ignoring().antMatchers("/js/**", "/css/**", "images");
	}

}
